<template>
  <div class="dashboard-container">
    <!-- 多条件搜索 -->
    <el-form :inline="true" :model="teacher" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="teacher.name" placeholder="讲师名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="teacher.level" placeholder="讲师头衔">
          <el-option label="首席讲师" value="2"></el-option>
          <el-option label="高级讲师" value="1"></el-option>
        </el-select>
      </el-form-item>
      <!-- 时间行 -->
      <el-form-item>
        <el-form-item>
          <el-date-picker
            type="date"
            placeholder="选择开始日期"
            v-model="teacher.gmtCreate"
            style="width: 100%"
            value-format="yyyy-MM-dd"
            default-time="00:00:00"
          ></el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-date-picker
            placeholder="选择截止日期"
            v-model="teacher.gmtModified"
            style="width: 100%"
            value-format="yyyy-MM-dd"
            default-time="00:00:00"
          ></el-date-picker>
        </el-form-item>
      </el-form-item>
      <!-- 操作行 -->
      <el-form-item>
        <el-button type="primary" @click="fetchData()">查询</el-button>
      
      <!-- 清空按钮 -->

      <el-button type="danger" icon="el-icon-delete" plain @click="resetData()"
        >清空
      </el-button>
      </el-form-item>
    </el-form>
    <!-- 讲师列表 -->
    <el-table
      :data="rows"
      style="width: 100%"
      border
      highlight-current-row
      v-loading="loading"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.8)"
    >
      <el-table-column
        type="selection"
        label="全选"
        width="55"
        
      ></el-table-column>
      <el-table-column type="index" label="序号" width="80"></el-table-column>
      <el-table-column
        prop="name"
        label="讲师名称"
        width="180"
      ></el-table-column>
      <el-table-column prop="level" label="讲师头衔" width="180">
        <template slot-scope="scope">
          <span v-if="scope.row.level == 1">高级讲师</span>
          <span v-if="scope.row.level == 2">首席讲师</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="intro"
        label="讲师简介"
        width="380"
      ></el-table-column>
      <el-table-column
        prop="gmtCreate"
        label="开始时间"
        width="200"
      ></el-table-column>
      <el-table-column
        prop="gmtModified"
        label="更新时间"
        width="200"
      ></el-table-column>
      <el-table-column prop="sort" label="排序" width="100"></el-table-column>
      <!-- 修改 删除 操作 -->
      <el-table-column label="操作" width="350">
        <template slot-scope="scope">
          <!-- :to 接变量 to 接常量 -->
          <router-link :to="'/teacher/updateTeacher/' + scope.row.id">
            <el-button type="primary">修改</el-button>
          </router-link>

          <el-button type="danger" @click="deleteTeacher(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <center>
      <el-pagination
        background
        :page-size="limit"
        layout="total,prev, pager, next"
        :total="total"
        :current-page="currentPage"
        @current-change="fetchData"
      >
      </el-pagination>
    </center>
  </div>
</template>

<script>
import teacherApi from "@/api/teacher";
export default {
  data() {
    return {
      currentPage: 1,
      limit: 10,
      teacher: {
        name: "",
        level: "",
        gmtCreate: "",
        gmtModifi: "",
      },
      rows: [],
      total: 0,
      loading: true,
    };
  },
  created() {
    this.showTeacher();
  },
  methods: {
    showTeacher() {
      teacherApi
        .findTeacherByPage(this.currentPage, this.limit, this.teacher)
        .then((res) => {
          this.total = res.data.total;
          this.rows = res.data.rows;
          this.loading = false
        })
        .catch((err) => {});
    },
    fetchData(page = 1) {
      this.currentPage = page;
      console.log(this.currentPage);
      teacherApi
        .findTeacherByPage(this.currentPage, this.limit, this.teacher)
        .then((res) => {
          this.total = res.data.total;
          this.rows = res.data.rows;
        })
        .catch((err) => {});
    },
    resetData() {
      this.teacher = {};
      this.showTeacher();
    },

    deleteTeacher(id) {
      this.$confirm("是否要删除该讲师信息", "删除讲师", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          console.log(id);
          teacherApi
            .deleteTeacher(id)
            .then((res) => {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.fetchData();
            })
            .catch((err) => {
              this.$message({
                type: "success",
                message: "删除失败!",
              });
            });
        })
        .catch(() => {});
    },
  },
};
</script>




<style>
</style>